package leetcode;

import util.Tree;
import util.TreeNode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0617.java
 * @Author : WangRuoyu
 * @Date 2023/10/23 20:14
 */


public class Q0617 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 != null && root2 != null) {
            root1.val += root2.val;
            root1.left = mergeTrees(root1.left, root2.left);
            root1.right = mergeTrees(root1.right, root2.right);
            return root1;
        } else if (root1 != null) {
            return root1;
        } else if (root2 != null) {
            return root2;
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        Q0617 sl = new Q0617();
        String[] t1 = {"1", "3", "5", null, null, null, "2", null, null};
        String[] t2 = {"2", "1", null, "4", null, null, "3", null, "7", null, null};
        TreeNode node1 = Tree.createTreeByPreorder(t1);
        TreeNode node2 = Tree.createTreeByPreorder(t2);
        Tree.preorderTraversal(sl.mergeTrees(node1, node2));
    }
}